cmake_minimum_required(VERSION 3.6)

project(cupp LANGUAGES Fortran)

set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_BINARY_DIR})
if (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
  list(APPEND fortran_flags
    -ffree-line-length-none
    -fno-range-check
    -fallow-argument-mismatch
    -fallow-invalid-boz
  )
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    list(APPEND fortran_flags -O0 -fcheck=all -g -fbacktrace)
  else ()
    list(APPEND fortran_flags -Ofast)
  endif ()
  # FIXME: OpenMP causes sigmentation faults.
  # list(APPEND fortran_flags -fopenmp)
elseif (CMAKE_Fortran_COMPILER_ID STREQUAL "Intel")
  if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    list(APPEND fortran_flags
      -no-wrap-margin
      -O0
      -check bounds
      -g
      -traceback
      -fp-model precise
    )
  else ()
    list(APPEND fortran_flags
      -no-wrap-margin
      -O3
      -align array64byte
      # -fp-model precise
    )
  endif ()
  if (CMAKE_Fortran_COMPILER_VERSION GREATER 2021.0)
    list(APPEND fortran_flags -qmkl=sequential)
  else ()
    list(APPEND fortran_flags -mkl=sequential)
  endif ()
  list(APPEND fortran_flags -qopenmp)
endif ()
string(REPLACE ";" " " CMAKE_Fortran_FLAGS "${fortran_flags}")

add_library(cupp_earth_atm)
target_sources(cupp_earth_atm PRIVATE
  src/cupp_const_mod.F90
  src/cupp_math_mod.F90
  src/cupp_mesh_mod.F90
  src/earth/atm/atm_formula_mod.F90
  src/earth/atm/atm_const_mod.F90
  src/earth/atm/atm_types_mod.F90
  src/earth/atm/tracer_types_mod.F90
  src/earth/atm/tracer_mod.F90
  src/earth/atm/cu/cu_const_mod.F90
  src/earth/atm/cu/cu_tdk_mod.F90
  src/earth/atm/cu/cu_gf_mod.F90
  src/earth/atm/gwd/gwd_const_mod.F90
  src/earth/atm/mp/mp_const_mod.F90
  src/earth/atm/mp/mp_lin_mod.F90
  src/earth/atm/pbl/pbl_const_mod.F90
  src/earth/atm/pbl/pbl_ysu_mod.F90
  src/earth/atm/sfc/sfc_const_mod.F90
)
